package cn.com.haopy.yue.ai.db.service.hos.impl;


import cn.com.haopy.yue.ai.db.entity.arisk.HOSUser;
import cn.com.haopy.yue.ai.db.mapper.arisk.HOSUserMapper;
import cn.com.haopy.yue.ai.db.service.hos.HOSUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;


/**
 * @author keel
 * @date 2022/4/13
 */
@Service
public class HOSUserServiceImpl extends ServiceImpl<HOSUserMapper, HOSUser> implements HOSUserService {
    @Override
    public Page<HOSUser> querySelective(String nickName, String mobile, Integer pageNo, Integer limit, String sort, String order) {
        QueryWrapper<HOSUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(nickName)) {
            queryWrapper.lambda().like(HOSUser::getNickname, "%" + nickName + "%");
        }
        if (StringUtils.hasText(mobile)) {
            queryWrapper.lambda().eq(HOSUser::getMobile, mobile);
        }
        queryWrapper.lambda().eq(HOSUser::getStatus, 0);
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        Page<HOSUser> page = new Page<>(pageNo, limit);
        return super.page(page, queryWrapper);
    }

    @Override
    public Map<String, Integer> getCount() {
        Map<String, Integer> map = new HashMap<>();
        // 注册用户
        LambdaQueryWrapper<HOSUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(HOSUser::getStatus, 0);
        int allCount = this.count(lambdaQueryWrapper);
        map.put("allCount", allCount);

        // 实名认证
        // weixinOpenid不为空的字符串以及不为null
        LambdaQueryWrapper<HOSUser> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.ne(HOSUser::getWeixinOpenid, "").isNotNull(HOSUser::getWeixinOpenid);
        int authCount = this.count(lambdaQueryWrapper1);
        map.put("authCount", authCount);

        // 今日注册
        LambdaQueryWrapper<HOSUser> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        LocalDate today = LocalDate.now();
        lambdaQueryWrapper2
                .ge(HOSUser::getCreateTime, today.atStartOfDay())
                .lt(HOSUser::getCreateTime, today.plusMonths(1).atStartOfDay());
        int todayCount = this.count(lambdaQueryWrapper2);
        map.put("todayCount", todayCount);

        // 今日认证
        LambdaQueryWrapper<HOSUser> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3
                .ge(HOSUser::getCreateTime, today.atStartOfDay())
                .lt(HOSUser::getCreateTime, today.plusMonths(1).atStartOfDay())
                .ne(HOSUser::getWeixinOpenid, "")
                .isNotNull(HOSUser::getWeixinOpenid);
        int todayAuthCount = this.count(lambdaQueryWrapper3);
        map.put("todayAuthCount", todayAuthCount);
        return map;
    }
}
